// TCPSocketServerDlg.cpp

#include "stdafx.h"
#include "TCPSocketServerDlg.h"
#include "afxdialogex.h"
#include "TCPSocketServer.h"

TCPSocketServerDlg* TCPSocketServerDlg::m_pSocketServerDlg = nullptr;

IMPLEMENT_DYNAMIC(TCPSocketServerDlg, CDialogEx)
BEGIN_MESSAGE_MAP(TCPSocketServerDlg, CDialogEx)
    ON_BN_CLICKED(IDC_CANCEL_SERVER, &TCPSocketServerDlg::OnBnClickedCancelServer)
    ON_BN_CLICKED(IDC_OPEN_SERVER, &TCPSocketServerDlg::OnBnClickedOpenServer)
    ON_BN_CLICKED(IDC_CLOSE_SERVER, &TCPSocketServerDlg::OnBnClickedCloseServer)
END_MESSAGE_MAP()

TCPSocketServerDlg::TCPSocketServerDlg(CWnd* pParent/* = NULL*/)
    : CDialogEx(TCPSocketServerDlg::IDD, pParent)
{
}

TCPSocketServerDlg::~TCPSocketServerDlg()
{
}

void TCPSocketServerDlg::UnInitialize()
{
    if (m_spTCPSocketServer.get() != nullptr)
    {
        AfxMessageBox(L"Please close server TCP.");

        return;
    }

    if (m_pSocketServerDlg != nullptr)
    {
        delete m_pSocketServerDlg;
        m_pSocketServerDlg = nullptr;
    }
}

void TCPSocketServerDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

TCPSocketServerDlg* TCPSocketServerDlg::GetInstance(CWnd* pParent/* = NULL*/)
{
    if (m_pSocketServerDlg == nullptr)
    {
        m_pSocketServerDlg = new TCPSocketServerDlg(pParent);
    }

    return m_pSocketServerDlg;
}

BOOL TCPSocketServerDlg::IsInitialize()
{
    if (m_pSocketServerDlg == nullptr)
    {
        return FALSE;
    }

    return TRUE;
}

void TCPSocketServerDlg::SetCloseCallBack(CLOSE_SERVER_DIALOG_CALLBACK callbackCloseDialog)
{
    m_callbackCloseDialog = callbackCloseDialog;
}

BOOL TCPSocketServerDlg::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message==WM_KEYDOWN)
    {
        if((pMsg->wParam == VK_RETURN) || (pMsg->wParam == VK_ESCAPE))
        {
            UnInitialize();

            return true;
        }
    }

    return CDialogEx::PreTranslateMessage(pMsg);
}


void TCPSocketServerDlg::OnBnClickedOpenServer()
{
    m_spTCPSocketServer.reset(new TCPSocketServer());
    DWORD dw = m_spTCPSocketServer.get()->OpenTCP(this, "127.0.0.1", 10001);
    if (dw == 1)
    {
        AfxMessageBox(L"Open TCP success.");
    }
    else if (dw == -1)
    {
        AfxMessageBox(L"OpenTCP() Parameter error.");
    }
    else if (dw == -2)
    {
        AfxMessageBox(L"WSAStartup() failed.");
    }
    else if (dw == -3)
    {
        AfxMessageBox(L"socket() failed.");
    }
    else if (dw == -4)
    {
        AfxMessageBox(L"bind() failed.");
    }
    else if (dw == -5)
    {
        AfxMessageBox(L"listen() failed.");
    }
    else
    {
        AfxMessageBox(L"Undefined error.");
    }

    GetDlgItem(IDC_OPEN_SERVER)->EnableWindow(FALSE);
    GetDlgItem(IDC_CLOSE_SERVER)->EnableWindow(TRUE);
    GetDlgItem(IDC_CANCEL_SERVER)->EnableWindow(FALSE);
}

void TCPSocketServerDlg::OnBnClickedCloseServer()
{
    DWORD dw = m_spTCPSocketServer.get()->CloseTCP();
    if (dw == 1)
    {
        m_spTCPSocketServer.release();
        AfxMessageBox(L"Close TCP success.");
    }
    else if (dw == -1)
    {
        AfxMessageBox(L"closesocket() failed.");
    }
    else if (dw == -2)
    {
        AfxMessageBox(L"WSACleanup() failed.");
    }
    else
    {
        AfxMessageBox(L"Undefined error.");
    }

    GetDlgItem(IDC_OPEN_SERVER)->EnableWindow(TRUE);
    GetDlgItem(IDC_CLOSE_SERVER)->EnableWindow(FALSE);
    GetDlgItem(IDC_CANCEL_SERVER)->EnableWindow(TRUE);
}

void TCPSocketServerDlg::OnBnClickedCancelServer()
{
    m_callbackCloseDialog();

    UnInitialize();
}
